import { getFiles } from '../fn/getFiles'; // 导入需要测试的函数
const path = require('path');
const fs = require('fs');

describe('getFiles', () => {
  it('method is openFile && type is img, return image files', () => {
    const folderPath = ['\\path\\to\\folder\\file1.jpg'];
    const type = 'img';
    const method = 'openFile';

    getFiles(folderPath, type, method).then(result => {
      expect(result).toEqual([
        { name: 'file1.jpg', path: '\\path\\to\\folder\\file1.jpg' },
      ]);
    })
  });

  it('method is openFile && type is img, return no image', () => {
    const folderPath = ['\\path\\to\\folder\\file1.txt'];
    const type = 'img';
    const method = 'openFile';

    getFiles(folderPath, type, method).then(result => {
      expect(result).toEqual([]);
    })
  });

  it('method is multiSelections && type is img, return image files', () => {
    const folderPath = ['\\path\\to\\folder\\file1.jpg', '\\path\\to\\folder\\file2.png', '\\path\\to\\folder\\file3.txt'];
    const type = 'img';
    const method = 'multiSelections';

    getFiles(folderPath, type, method).then(result => {
      expect(result).toEqual([
        { name: 'file1.jpg', path: '\\path\\to\\folder\\file1.jpg' },
        { name: 'file2.png', path: '\\path\\to\\folder\\file2.png' },
      ]);
    })
  });

  it('method is openDirectory && type is img, return image files', () => {
    const folderPath = ['\\path\\to\\folder'];
    const type = 'img';
    const method = 'openDirectory';

    jest.spyOn(require('fs'), 'readdirSync').mockReturnValue(['file1.png', 'file2.doc']);

    jest.spyOn(require('path'), 'join').mockReturnValue(['\\path\\to\\folder\\file1.png', '\\path\\to\\folder\\file2.doc']);

    getFiles(folderPath, type, method).then(result => {
      expect(result).toEqual([
        { name: 'file1.png', path: '\\path\\to\\folder\\file1.png' }
      ]);
    })
  });

  it('method is openDirectory && type is img, return empty files', () => {
    const folderPath = ['\\path\\to\\folder'];
    const type = 'img';
    const method = 'openDirectory';

    jest.spyOn(require('fs'), 'readdirSync').mockReturnValue(['file1.txt', 'file2.doc']);

    getFiles(folderPath, type, method).then(result => {
      expect(result).toEqual([]);
    })
  });
});


